package com.ymatou.utils.database;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MyBatisUtil {
    private SqlSessionFactory sqlSessionFactory = null;
    private SqlSession sess;
    private String openenv = "";

    public MyBatisUtil() {
    }

    public SqlSession initialFactory(String env) throws IOException {
        try {
            if (this.openenv != env) {
                String resource = System.getProperty("user.dir") + File.separator + "config" + File.separator + "dbconfig.xml";
                this.sqlSessionFactory = (new SqlSessionFactoryBuilder()).build(new FileInputStream(new File(resource)), env);
                this.sess = this.sqlSessionFactory.openSession();
                this.openenv = env;
            }

            return this.sess;
        } catch (Exception var3) {
            var3.printStackTrace();
            return this.sess;
        }
    }

    public void close() {
        this.openenv = null;
        this.sess.close();
    }
}
